import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg';
import { join } from 'path';

export default (appInfo: EggAppInfo) => {
  const config = {} as PowerPartial<EggAppConfig>;

  // override config from framework / plugin
  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1652664843220_2457';

  // add your egg config in here
  // config.middleware = ['customError'];

  config.security = {
    csrf: {
      enable: false,
    },
  };

  config.mongoose = {
    url: 'mongodb://sps.soft.net:27017/lego',
    options: {
      useUnifiedTopology: true,
    },
  };

  config.bcrypt = {
    saltRounds: 10,
  };

  config.jwt = {
    enable: true,
    secret: process.env.JWT_SECRET,
    match: ['/api/users/current', '/api/works'],
  };

  config.redis = {
    client: {
      port: 6379,
      host: '127.0.0.1',
      password: '',
      db: 0,
    },
  };

  config.cors = {
    origin: 'http://localhost:8080',
    allowMethods: 'GET,HEAD,PUT,OPTIONS.POST,DELETE,PATCH',
  };

  config.multipart = {
    whitelist: ['.png', '.jpg', '.gif', '.webp'],
    fieldSize: '50kb',
  };

  config.static = {
    dir: [
      { prefix: '/public', dir: join(appInfo.baseDir, 'app/public') },
      { prefix: '/uploads', dir: join(appInfo.baseDir, 'uploads') },
    ],
  };

  config.view = {
    defaultViewEngine: 'nunjucks',
  };

  // add your special config in here
  const bizConfig = {
    sourceUrl: `https://github.com/eggjs/examples/tree/master/${appInfo.name}`,
    H5BaseUrl: 'http://localhost:7001/api/pages',
  };

  // the return config will combines to EggAppConfig
  return {
    ...(config as {}),
    ...bizConfig,
  };
};
